package com.kzd.btgame.jetpack.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.kzd.btgame.base.BaseViewModel
import com.kzd.btgame.entity.*
import com.kzd.btgame.jetpack.api.*
import com.module.lemlin.http.*
import com.module.lemlin.owner.setRxLifeScope

class MoneyViewModel(application: Application) : BaseViewModel(application) {

    private var page: Int = 1
    private var limit: Int = 50

    val moneyDataLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<MoneyData>>>()
    fun moneyData() {
        setRxLifeScope(SubscribeViewModel(moneyDataLiveData)) {
            userMoneyData()
        }
    }

    val moneySignLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<MoneySign>>>()
    fun moneySign() {
        setRxLifeScope(SubscribeViewModel(moneySignLiveData)) {
            personalGetSignInConfig()
        }
    }

    val moneyTaskNewLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<MoneyTaskNew>>>()
    fun moneyTaskNew() {
        setRxLifeScope(SubscribeViewModel(moneyTaskNewLiveData)) {
            userGetNewbieTask()
        }
    }

    val moneyTaskNewGetLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun moneyTaskNewGet(type: String) {
        setRxLifeScope(SubscribeViewModel(moneyTaskNewGetLiveData)) {
            userNewbieReceive(type)
        }
    }

    val moneyTaskAddShareLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun moneyTaskAddShare() {
        setRxLifeScope(SubscribeViewModel(moneyTaskAddShareLiveData)) {
            userAddShare()
        }
    }

    val moneyTaskLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<MoneyTask>>>()
    fun moneyTaskDay() {
        setRxLifeScope(SubscribeViewModel(moneyTaskLiveData)) {
            userGetTodayTask()
        }
    }

    val moneyTaskDayGetLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun moneyTaskDayGet(type: String) {
        setRxLifeScope(SubscribeViewModel(moneyTaskDayGetLiveData)) {
            userTodayReceive(type)
        }
    }

    val moneySignSubmitLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun moneySignSubmit() {
        setRxLifeScope(SubscribeViewModel(moneySignSubmitLiveData)) {
            personalSignIn()
        }
    }

    val moneyTaskPlayLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<MoneyTaskPlay>>>()
    fun moneyTaskPlay(isLoadMore: Boolean = false) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<MoneyTaskPlay>>(moneyTaskPlayLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<MoneyTaskPlay>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            personalTaskList(page.toString(), limit.toString())
        }
    }

    val moneyTaskPlayGetLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun moneyTaskPlayGet(id: String) {
        setRxLifeScope(SubscribeViewModel(moneyTaskPlayGetLiveData)) {
            personalTaskChange(id)
        }
    }

    val moneyTaskMallLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<MoneyTaskMall>>>()
    fun moneyTaskMall() {
        setRxLifeScope(SubscribeViewModel(moneyTaskMallLiveData)) {
            productMall()
        }
    }

    val moneyTaskMallDetailsLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataT<MallDetails>>>()
    fun moneyTaskMallDetails(id: String) {
        setRxLifeScope(SubscribeViewModel(moneyTaskMallDetailsLiveData)) {
            appMallInfo(id)
        }
    }

    val moneyTaskMallQueryLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun moneyTaskMallQuery(id: String) {
        setRxLifeScope(SubscribeViewModel(moneyTaskMallQueryLiveData)) {
            productMallQuery(id)
        }
    }

    val moneyTaskMallSubmitLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<String>>>()
    fun moneyTaskMallSubmit(goodId: String, addressId: String, num: Int) {
        setRxLifeScope(SubscribeViewModel(moneyTaskMallSubmitLiveData)) {
            appMallBuy(goodId, addressId, num.toString())
        }
    }

    val moneyBossSharedLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<MoneyBossShared>>>()
    fun moneyBossShared() {
        setRxLifeScope(SubscribeViewModel(moneyBossSharedLiveData)) {
            userShareOrderList()
        }
    }

    val moneyBossInviteLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<MoneyBossInvite>>>()
    fun moneyBossInvite() {
        setRxLifeScope(SubscribeViewModel(moneyBossInviteLiveData)) {
            userInviteNotice()
        }
    }


    val moneyExperienceLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataE<MoneyExperience>>>()
    fun moneyExperience(isLoadMore: Boolean = false) {
        setRxLifeScope(object :
            SubscribeViewModel<HttpResponseDataE<MoneyExperience>>(moneyExperienceLiveData) {
            override fun onSuccess(resp: HttpResponseDataE<MoneyExperience>) {
                super.onSuccess(resp.apply {
                    this.isLoadMore = isLoadMore
                })
                page += 1
            }
        }) {
            if (!isLoadMore) {
                page = 1
            }
            userGetExperienceCoin(page.toString(), limit.toString())
        }
    }


}